package com.dji.GSDemo.GaodeMap;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dji.GSDemo.GaodeMap.entity.Task;
import com.dji.GSDemo.GaodeMap.service.MissionService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/*
任务列表界面：负责显示任务的简单信息
 */

public class taskListActivity extends Activity {
    private MissionService ms=new MissionService();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //允许主线程访问网络
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        //设置列表样式
        setContentView(R.layout.layout_bottom_sheet);
        //填充列表
        createSheetView();

    }



    //填充列表
    private void createSheetView() {

        List<Map<String,Object>> data=putData();
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.list_item,
                new String[]{"tsId","tsName","tsTime"},new int[]{R.id.tsId,R.id.tsName,R.id.tsTime});
        ListView listview  = findViewById(R.id.mylistview);
        listview.setAdapter(simpleAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               //Toast.makeText(taskListActivity.this,data.get(position).get("tsName").toString(),Toast.LENGTH_SHORT).show();
                //设置路由，跳转页面
               Intent intent = new Intent(taskListActivity.this,taskActivity.class);
               int pathId= (int)data.get(position).get("pathId");
                int taskId= (int)data.get(position).get("tsId");
               intent.putExtra("pathId",pathId);
               intent.putExtra("taskId",taskId);
               startActivity(intent);
            }
        });

    }
   // 获取任务列表
    public List<Map<String,Object>> putData(){
        List<Map<String,Object>> result=new ArrayList<>();
        List<Task> tasks= ms.getUnFinishedTaskList(1,10);
        for(Task task:tasks){
            Map<String,Object> map=new HashMap<>();
            map.put("tsId",task.getTaskId());
            map.put("tsName",task.getTaskName());
            map.put("tsTime",task.getCreatetime());
            map.put("pathId",task.getFlyingpathId());
            result.add(map);
        }
        return result;
    }
}
